<?php
class ControllerSurveySurvey extends Controller { 
	private $error = array();

	public function index() {
		$this->load->language('survey/survey');

		$this->document->setTitle($this->language->get('heading_title'));
		 
		$this->load->model('survey/survey');

		$this->getList();
	}

	public function insert() {
		$this->load->language('survey/survey');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('survey/survey');
				
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			
			$this->model_survey_survey->addSurvey($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('survey/survey');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('survey/survey');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_survey_survey->editSurvey($this->request->get['survey_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('common/home', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}
 
	public function delete() {
		$this->load->language('survey/survey');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('survey/survey');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $survey_id) {
				$this->model_survey_survey->deleteSurvey($survey_id);
			}
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('survey/survey/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['delete'] = $this->url->link('survey/survey/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');	

		$this->data['surveys'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$survey_total = $this->model_survey_survey->getTotalSurveys();
	
		$results = $this->model_survey_survey->getSurveys($data);
 
    	foreach ($results as $result) {
			$action = array();
			$survey_stats = array();
			 
			$action[] = array(
				'text' => $this->language->get('text_view'),
				'href' => $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . '&survey_id=' . $result['survey_id'] . $url, 'SSL')
			);
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('survey/survey/update', 'token=' . $this->session->data['token'] . '&survey_id=' . $result['survey_id'] . $url, 'SSL')
			);
			
			$action[] = array(
				'text' => $this->language->get('text_question'),
				'href' => $this->url->link('survey/question/insert', 'token=' . $this->session->data['token'] . '&survey_id=' . $result['survey_id'] . $url, 'SSL')
			);
			
			$action[] = array(
				'text' => $this->language->get('text_default'),
				'href' => $this->url->link('survey/survey/setDefault', 'token=' . $this->session->data['token'] . '&survey_id=' . $result['survey_id'] . $url, 'SSL')
			);
			
			$total_guest = $this->model_survey_survey->getTotalGuestBySurveyId($result['survey_id']);
			$total_customer = $this->model_survey_survey->getTotalCustomerBySurveyId($result['survey_id']);
			$total_participent = $this->model_survey_survey->getTotalParticipentBySurveyId($result['survey_id']);
			
			
			$survey_stats[] = array(
				'text' => 'Customer:',
				'value' => $total_customer 				
			);
			
			$survey_stats[] = array(
				'text' => 'Guest:',
				'value' => $total_guest 				
			);
			
			$survey_stats[] = array(
				'text' => 'Total:',
				'value' => $total_participent // ($total_customer + $total_guest) 
			);
						
			$this->data['surveys'][] = array(
				'survey_id' => $result['survey_id'],
				'title'          => $result['title'],
				'sort_order'     => $result['sort_order'],
				'stats'     => $survey_stats,
				'selected'       => isset($this->request->post['selected']) && in_array($result['survey_id'], $this->request->post['selected']),
				'action'         => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_stats'] = $this->language->get('column_survey_stats');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_id'] = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . '&sort=i.survey_id' . $url, 'SSL');
		$this->data['sort_title'] = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . '&sort=id.title' . $url, 'SSL');
		$this->data['stats'] = 'javascript:void(0);';
		$this->data['sort_sort_order'] = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . '&sort=i.sort_order' . $url, 'SSL');
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/survey_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}
	
	public function participents() {
		
		$this->load->language('survey/survey');

		$this->load->model('survey/survey');
		
		$survey_id = $this->request->get['survey_id'];
		
		$question_id = NULL;
		if(isset($this->request->get['filter_answer']))
			$question_id = $this->request->get['question_id'];
		
		$filter_answer = NULL;
		if(isset($this->request->get['filter_answer']))
			$filter_answer = $this->request->get['filter_answer'];
		
		$survey_jnfo = $this->model_survey_survey->getSurvey($survey_id);
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
		
		if (isset($this->request->get['filter_answer'])) {
			$url .= '&filter_answer=' . $this->request->get['filter_answer'];
		}
		
		if (isset($this->request->get['question_id'])) {
			$url .= '&question_id=' . $this->request->get['question_id'];
		}
		
		if (isset($this->request->get['survey_id'])) {
			$url .= '&survey_id=' . $this->request->get['survey_id'];
		}
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . '&survey_id='. $survey_id, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_participent'),
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
							
		$this->data['back'] = $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . '&survey_id='. $survey_id, 'SSL');			

		$this->data['surveys'] = array();
		
		$question_data = array(
			'filter_survey_id' => $survey_id,
			'filter_question_id' => $question_id,
			'filter_answer' => (!is_null($filter_answer)) ? urldecode($filter_answer) : $filter_answer 
  		);

  		
//  		print_r($question_data);
 		
// 		$results = array_merge($total_guests, $total_customers);
 		$results =  $this->model_survey_survey->getParticipents($question_data);
 		$survey_total = count($results);
		
		$this->data['questions'] = array();
		
// 		print_r($results);

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'] - 1;
			if($page > 1) {
				$counter = ($page * $this->config->get('config_admin_limit'));
				$counter = $counter + 1;
			} else {
				$counter = 1;	
			}
				
		} else {
			$counter = 1;
		}
 		
    	foreach ($results as $result) {
    		$action = array();
			
			$action[] = array(
				'text' => 'View Answers',
				'href' => $this->url->link('survey/survey/answers', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&user_id=' . $result['customer_id'] . $url, 'SSL')
			);		

				
			$this->data['questions'][] = array(
				'customer_id' => $result['customer_id'],
				'name'          => $result['name'],
				'type'          => ($result['guest_id']) ? 'Guest' : 'Customer',
				'sort_order'          => $counter,
				'action'          => $action,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
			
			$counter++;
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_participent');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['column_type'] = $this->language->get('column_type');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/participents.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());	
	
	}
	
	public function answers() {
		
		$this->load->language('survey/survey');

		$this->load->model('survey/survey');
		
		$survey_id = $this->request->get['survey_id'];
		
		$question_id = NULL;
		if(isset($this->request->get['question_id']))
			$question_id = $this->request->get['question_id'];
		
		
		$filter_answer = NULL;
		if(isset($this->request->get['question_id']))
			$filter_answer = $this->request->get['filter_answer'];		
		
		$user_info = array();
		$customer_id = NULL;
		if(isset($this->request->get['user_id'])) {
			$customer_id = $this->request->get['user_id'];
			
			$this->load->model('sale/customer');
			$user_info = $this->model_sale_customer->getCustomer($customer_id);	
		}			
		
		if(empty($customer_id))
			die("customer id missing.");

		
//		print_r($user_info);
		
		$survey_jnfo = $this->model_survey_survey->getSurvey($survey_id);
		$heading_title = $user_info['firstname'] . " " . $user_info['lastname'] . " (" . $user_info['email'] . ") ";
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
		
		if (isset($this->request->get['filter_answer'])) {
			$url .= '&filter_answer=' . $this->request->get['filter_answer'];
		}
		
		if (isset($this->request->get['question_id'])) {
			$url .= '&question_id=' . $this->request->get['question_id'];
		}
		
		if (isset($this->request->get['survey_id'])) {
			$url .= '&survey_id=' . $this->request->get['survey_id'];
		}
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Participents',
			'href'      => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $heading_title,
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
							
		$this->data['back'] = $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . $url, 'SSL');			

		$this->data['surveys'] = array();

		$data = array(
			'filter_survey_id' => $survey_id,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')			
		);
		
		$survey_total = $this->model_survey_question->getTotalQuestions($data);
	
		$results = $this->model_survey_question->getQuestions($data);
		
//		print_r($results);		
 
    	foreach ($results as $result) {
    		$answer = array();
    		
    		$question_id = $result['question_id'];
    		
    		$options = array();
    		$question_data = array();
    		
    		$question_info = $result;
      		$question_id = $question_info['question_id'];
      		
      		switch($question_info['qtype']) {
      		case 'text':
      		case 'textarea':
	      		$question_data = array(
	  				'filter_survey_id' => $survey_id,
	  				'filter_question_id' => $question_id,
	  				'filter_check_answer' => 'not-empty',
	  				'filter_customer_id' => $customer_id
	      		);
      			
      			$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
      			
      			$get_answer = "";
      			if($total_participant) {
      				$get_answer = $this->model_survey_survey->getAnswer($question_data);
      				$get_answer = $get_answer['answer'];
      			}
	      		
	      		$answer[] = array(
					'text' => $get_answer,
					'value' => $total_participant				
				);
      		break;
      		case 'checkbox':
      		case 'radio':
	      		$question_info['lable'] = str_replace("\r","", $question_info['lable']);
	      		$all_lables = explode("\n", $question_info['lable']);
	      		if(!empty($question_info['value'])) {
	      			$question_info['value'] = str_replace("\r","", $question_info['value']);
	      			$all_values = explode("\n", $question_info['value']);
	      		} else {
	      			$all_values = $all_lables;
	      		}      			
	      		
	      		if(!empty($check_answer['answer']))
	      			$default_value = $check_answer['answer'];
	      		else
	      			$default_value = $question_info['default'];
	      		
	      		foreach($all_lables as $key => $each_value) {
	      			      			
	      			$default = 0;
	      			if($default_value == $all_values[$key])
	      				$default = 1;
	      				
	      		    $options[] = array(
		      			'lable' => $all_lables[$key],
		      			'value' => $all_values[$key],
		      			'default' => $default
		      		);	
	      		}
	      		
	      		foreach($options as $each_option) {
	      			
	      			$question_data = array(
	      				'filter_survey_id' => $survey_id,
	      				'filter_question_id' => $question_id,
	      				'filter_customer_id' => $customer_id,      				
	      				'filter_answer' => $each_option['value']
	      			);
	      			
	      			$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
	
					$answer[] = array(
						'text' => $each_option['lable'],
						'value' => $total_participant,
						'link' => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . $each_option['value'])
					);      			
	      		}
	      		
    			break;
    			case 'default' :
    			case 'lable' :
    			$answer = array();
    			break;
    		}
				
			$this->data['questions'][] = array(
				'question_id' => $result['question_id'],
				'title'          => $result['title'],
				'sort_order'     => $result['sort_order'],
				'answer'     => $answer,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
		}	
	
		$this->data['heading_title'] = $heading_title;

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_id'] = 'javascript:void(0)';
		$this->data['sort_title'] = 'javascript:void(0)';
		$this->data['sort_stats'] = 'javascript:void(0)';
		$this->data['sort_sort_order'] = 'javascript:void(0)';
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/answers.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
		
	}
	
	public function setDefault() {
		$this->session->data['config_survey_id'] = $this->request->get['survey_id'];

		$url = '';
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
			
		$this->redirect($this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'));
	}
	
	public function info() {
		$this->load->language('survey/survey');

		$this->load->model('survey/survey');
		
		$survey_id = $this->request->get['survey_id'];
		
		if(empty($survey_id))
			die("survey id missing.");
		
		$survey_jnfo = $this->model_survey_survey->getSurvey($survey_id);
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
							
		$this->data['participents'] = $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . '&survey_id='.$survey_id, 'SSL');			
		$this->data['back'] = $this->url->link('common/home', 'token=' . $this->session->data['token'] . $url, 'SSL');			

		$this->data['surveys'] = array();

		$data = array(
			'filter_survey_id' => $survey_id,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')			
		);
		
		$survey_total = $this->model_survey_question->getTotalQuestions($data);
	
		$results = $this->model_survey_question->getQuestions($data);
		
//		print_r($results);		
 
    	foreach ($results as $result) {
    		$answer = array();
    		$options = array();
    		$question_data = array();
    		
    		$question_info = $result;
      		$question_id = $question_info['question_id'];
      		
      		switch($question_info['qtype']) {
      		case 'text':
      		case 'textarea':
      		
      			$question_data = array(
      				'filter_survey_id' => $survey_id,
      				'filter_question_id' => $question_id,
      				'filter_check_answer' => 'not-empty'
	      		);
	      			
	      		$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
	      		
	      		$check_answer = "not-empty";
	      		
	      		$answer[] = array(
					'text' => 'Have Some Text',
					'value' => $total_participant,
					'link' => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . $check_answer)				
				);
      		break;
      		case 'checkbox':
      		case 'radio':
	      		$question_info['lable'] = str_replace("\r","", $question_info['lable']);
	      		$all_lables = explode("\n", $question_info['lable']);
	      		if(!empty($question_info['value'])) {
	      			$question_info['value'] = str_replace("\r","", $question_info['value']);
	      			$all_values = explode("\n", $question_info['value']);
	      		} else {
	      			$all_values = $all_lables;
	      		}      			
	      		
	      		if(!empty($check_answer['answer']))
	      			$default_value = $check_answer['answer'];
	      		else
	      			$default_value = $question_info['default'];
	      		
	      		foreach($all_lables as $key => $each_value) {
	      			      			
	      			$default = 0;
	      			if($default_value == $all_values[$key])
	      				$default = 1;
	      				
	      		    $options[] = array(
		      			'lable' => $all_lables[$key],
		      			'value' => $all_values[$key],
		      			'default' => $default
		      		);	
	      		}
	      		
	      		foreach($options as $each_option) {
	      			
	      			$question_data = array(
	      				'filter_survey_id' => $survey_id,
	      				'filter_question_id' => $question_id,
	      				'filter_answer' => $each_option['value']
	      			);
	      			
	      			$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
	
					$answer[] = array(
						'text' => $each_option['lable'],
						'value' => $total_participant,
						'link' => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . urlencode($each_option['value']))
					);      			
	      		}
	      		
    			break;
    			case 'default' :
    			case 'lable' :
    			$answer = array();
    			break;
    		}
				
			$this->data['questions'][] = array(
				'question_id' => $result['question_id'],
				'title'          => $result['title'],
				'sort_order'     => $result['sort_order'],
				'answer'     => $answer,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
		}	
	
		$this->data['heading_title'] = $survey_jnfo['title'];

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_participents'] = $this->language->get('button_all_participents');
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_id'] = 'javascript:void(0)';
		$this->data['sort_title'] = 'javascript:void(0)';
		$this->data['sort_stats'] = 'javascript:void(0)';
		$this->data['sort_sort_order'] = 'javascript:void(0)';
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/survey_info.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	
	}

	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_closing'] = $this->language->get('entry_closing');
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_progress'] = $this->language->get('entry_progress');
		$this->data['entry_login'] = $this->language->get('entry_login');
		$this->data['entry_mode'] = $this->language->get('entry_mode');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		
		$this->data['text_mode_page'] = $this->language->get('text_mode_page');
		$this->data['text_mode_question'] = $this->language->get('text_mode_question');
		$this->data['text_progress_yes'] = $this->language->get('text_progress_yes');
		$this->data['text_progress_no'] = $this->language->get('text_progress_no');
		$this->data['text_login_yes'] = $this->language->get('text_login_yes');
		$this->data['text_login_no'] = $this->language->get('text_login_no');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
    	
		$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_design'] = $this->language->get('tab_design');
		
		$this->data['token'] = $this->session->data['token'];

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = array();
		}
		
	 	if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = array();
		}
		
		if (isset($this->error['closing'])) {
			$this->data['error_closing'] = $this->error['closing'];
		} else {
			$this->data['error_closing'] = array();
		}
		
		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),     		
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
							
		if (!isset($this->request->get['survey_id'])) {
			$this->data['action'] = $this->url->link('survey/survey/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('survey/survey/update', 'token=' . $this->session->data['token'] . '&survey_id=' . $this->request->get['survey_id'] . $url, 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('common/home', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['survey_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$survey_info = $this->model_survey_survey->getSurvey($this->request->get['survey_id']);
		}
		
		$this->load->model('localisation/language');
		$data = array(
			'filter_status' => 1 
		);		
		$this->data['languages'] = $this->model_localisation_language->getLanguages($data);
		
		if (isset($this->request->post['survey_description'])) {
			$this->data['survey_description'] = $this->request->post['survey_description'];
		} elseif (isset($this->request->get['survey_id'])) {
			$this->data['survey_description'] = $this->model_survey_survey->getSurveyDescriptions($this->request->get['survey_id']);
		} else {
			$this->data['survey_description'] = array();
		}

		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($survey_info)) {
			$this->data['status'] = $survey_info['status'];
		} else {
			$this->data['status'] = 1;
		}
		
		if (isset($this->request->post['mode'])) {
			$this->data['mode'] = $this->request->post['mode'];
		} elseif (isset($survey_info)) {
			$this->data['mode'] = $survey_info['mode'];
		} else {
			$this->data['mode'] = 'P';
		}
		
		if (isset($this->request->post['progress'])) {
			$this->data['progress'] = $this->request->post['progress'];
		} elseif (isset($survey_info)) {
			$this->data['progress'] = $survey_info['progress'];
		} else {
			$this->data['progress'] = 'N';
		}
		
		if (isset($this->request->post['login'])) {
			$this->data['login'] = $this->request->post['login'];
		} elseif (isset($survey_info)) {
			$this->data['login'] = $survey_info['login'];
		} else {
			$this->data['login'] = 'Y';
		}
		
		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		if (isset($this->request->post['survey_store'])) {
			$this->data['survey_store'] = $this->request->post['survey_store'];
		} elseif (isset($survey_info)) {
			$this->data['survey_store'] = $this->model_survey_survey->getSurveyStores($this->request->get['survey_id']);
		} else {
			$this->data['survey_store'] = array(0);
		}		
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($survey_info)) {
			$this->data['keyword'] = $survey_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (isset($survey_info)) {
			$this->data['sort_order'] = $survey_info['sort_order'];
		} else {
			
			$this->data['sort_order'] = $this->model_survey_survey->getNextSortOrder();	
			
			if(empty($this->data['sort_order'])) {
				$this->data['sort_order'] = '1';	
			}
		}
		
		if (isset($this->request->post['survey_layout'])) {
			$this->data['survey_layout'] = $this->request->post['survey_layout'];
		} elseif (isset($survey_info)) {
			$this->data['survey_layout'] = $this->model_survey_survey->getSurveyLayouts($this->request->get['survey_id']);
		} else {
			$this->data['survey_layout'] = array();
		}

		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
				
		$this->template = 'survey/survey_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'survey/survey')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['survey_description'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['title'])) < 3) || (strlen(utf8_decode($value['title'])) > 64)) {
				$this->error['title'][$language_id] = $this->language->get('error_title');
			}
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
			
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'survey/survey')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		foreach ($this->request->post['selected'] as $survey_id) {
			// put here all the checks for survey
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>